######### Notes ###########

# Demographics: All
#               Male, Female
#               16-24, 25-49, 50-64, 65+

# Other demographics not included
## Race (but non white categories very small n...)
## Class


######### BES Cross Sections ###########

library(tidyverse)
library(haven)

setwd("/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Survey Data/Originals/BES/BES Cross Sectional") 

## 1963, 1966, 1970

read_sav("BES_1963-1970.sav") %>%
  filter(v10 == 1) %>% # 1963
  select(v929, v938, v283,
    v224, v192a, v192b, v192c,v192d, v192e, v192f)  %>% # MIP questions code 54 = Crime general, 55 = Treat criminals firmly, Keep death penalty, Bring back the birch
  mutate(across(c(v192a, v192b, v192c,v192d, v192e, v192f), 
                ~ifelse(. %in% c(55, 54), 1, 
                        ifelse(is.na(.), NA_integer_, 0)))) %>%
  mutate(v224 = case_when(
    v224 == 1 ~ "support",
    v224 == 3 ~ "don't know",
    v224 == 5 ~ "oppose")) %>%
  rowwise() %>%
  mutate(MIP_crime = sum(v192a,v192b,v192c,v192d,v192e,v192f, na.rm = TRUE)) %>%
  mutate(MIP_crime = ifelse(is.na(v192a), NA_integer_, MIP_crime)) %>%
  rename(death_penalty = v224, gender = v929, age = v938) %>%
  mutate(race = case_match(v283, 1 ~ "white", c(2,3) ~ "non-white")) %>%
  mutate(weight = 1, year = 1963) %>%
  select(gender, age, race, weight, year, death_penalty, MIP_crime) %>%
  mutate(gender = case_when(gender == 1 ~ 'Male', gender == 2 ~ "Female")) -> surveydata


read_sav("BES_1963-1970.sav") %>%
  filter(v13 == 1) %>% #1966
  select(v225, v14, v284, v931, v940) %>%
  mutate(v225 = case_when(
    v225 == 1 ~ "support",
    v225 == 3 ~ "don't know",
    v225 == 5 ~ "oppose")) %>%
  rename(death_penalty = v225, weight = v14,
         gender = v931, age = v940, race = v284)%>%
  mutate(gender = case_when(gender == 1 ~ 'Male', gender == 2 ~ "Female")) %>%
  mutate(race = case_match(race, 1 ~ "non-white", 2 ~ "white")) %>%
  mutate(year = 1966) %>%
  bind_rows(surveydata, .) -> surveydata


read_sav("BES_1963-1970.sav") %>%
  filter(v15 == 1) %>% #1970
  select(v226, v16, v285, v932, v941) %>%
  mutate(v226 = case_when(
    v226 == 1 ~ "support",
    v226 == 3 ~ "don't know",
    v226 == 5 ~ "oppose")) %>%
  rename(death_penalty = v226, weight = v16,
         gender = v932, age = v941, race = v285) %>%
  mutate(gender = case_when(gender == 1 ~ 'Male', gender == 2 ~ "Female")) %>%
  mutate(race = case_match(race, 1 ~ "non-white", 2 ~ "white")) %>%
  mutate(year = 1970) %>%
  bind_rows(surveydata, .) -> surveydata


# BES_1974feb.sav - No relevant variables

read_sav("BES_1974oct.sav") %>% # seems to be no weighting variables and no race variable
  select(oct199, oct191, oct465, oct473) %>%
  mutate(weight = 1) %>%
  mutate(gender = case_when(oct199 == 1 ~ 'Male', oct199 == 2 ~ "Female")) %>%
  mutate(age = ifelse(oct191 %in% c(0, 1000), NA_integer_, 1974-oct191)) %>%
  mutate(gov_toughen_crime = ifelse(oct465 %in% c(0, 7), NA_integer_,
                                    tolower(as.character(as_factor(oct465))))) %>%
  mutate(MIGovAim = ifelse(oct473 %in% c(0), NA_integer_,
                            ifelse(oct473 == 3, 1, 0))) %>%
  select(gender, age, MIGovAim, gov_toughen_crime, weight) %>%
  mutate(year = 1974) %>%
  bind_rows(surveydata, .) -> surveydata



read_sav("BES_1979.sav") %>% # seems to be no weight or MIP question
  mutate(gender = case_when(m199288 == 1 ~ 'Male', m199288 == 5 ~ "Female")) %>%
  mutate(race = case_match(m000317, 1 ~ "white", 2 ~ "black", 3 ~ "asian", c(4,5,6,9) ~ "other")) %>%
  mutate(age = ifelse(m191279 %in% c(888), NA_integer_, 1979-m191279)) %>%
  mutate(stiffsentences = ifelse(m000140 %in% c(0), NA_integer_,
                                    tolower(as.character(as_factor(m000140))))) %>%
  mutate(death_penalty = ifelse(m000134 %in% c(0), NA_integer_,
                                 tolower(as.character(as_factor(m000134))))) %>%
  mutate(crim_imprt_vote = ifelse(m000075 %in% c(0), NA_integer_,
                                 tolower(as.character(as_factor(m000075))))) %>%
  mutate(weight = 1) %>%
  select(gender, age, race, weight, stiffsentences, death_penalty, crim_imprt_vote) %>%
  mutate(year = 1979) %>%
  bind_rows(surveydata, .) -> surveydata


read_dta("BES_1983.dta") %>%
  mutate(gender = case_when(q64b == 1 ~ 'Male', q64b == 2 ~ "Female")) %>%
  mutate(age = ifelse(q56 %in% c(0, 99), NA_integer_, q56)) %>%
  mutate(race = case_match(q64a, 1 ~ "white", 2 ~ "asian", 3 ~ "black", 4 ~ "other")) %>%
  mutate(death_penalty = ifelse(q42c %in% c(9), NA_integer_,
                                tolower(as.character(as_factor(q42c))))) %>%
  mutate(stiffsentences = ifelse(q42e %in% c(9), NA_integer_,
                                 tolower(as.character(as_factor(q42e))))) %>%
  mutate(crimerightscale = ifelse(q41a %in% c(95, 96, 99), NA_integer_,
                                  ifelse(q41a == 98, "dk", (q41a+10)/2))) %>% # -10 protect rights, 10 cut crime
  mutate(crim_imprt_vote = ifelse(q20a6 %in% c(9), NA_integer_,
                                  tolower(as.character(as_factor(q20a6))))) %>%
  mutate(weight = 1) %>%
  select(gender, age, race, weight, stiffsentences, death_penalty, crim_imprt_vote, crimerightscale) %>%
  mutate(year = 1983) %>%
  bind_rows(surveydata, .) -> surveydata


  
read_dta("BES_1987.dta") %>%
  mutate(gender = case_when(v58b == 1 ~ 'Male', v58b == 2 ~ "Female")) %>%
  mutate(age = ifelse(v58c %in% c(0, 98, 99), NA_integer_, v58c)) %>%
  mutate(race = case_match(v58a, 1 ~ "white", 2 ~ "asian", 3 ~ "black", 4 ~ "other")) %>%
  mutate(death_penalty = ifelse(v121n %in% c(9), NA_integer_,
                                tolower(as.character(as_factor(v121n))))) %>%
  mutate(stiffsentences = ifelse(v121l %in% c(9), NA_integer_,
                                 tolower(as.character(as_factor(v121l))))) %>%
  mutate(crimerightscale = ifelse(v39a %in% c(97, 99), NA_integer_, 
                                  ifelse(v39a == 95, 1,
                                         ifelse(v39a == 96, 11,
                                                ifelse(v39a == 98, "dk", v39a-1))))) %>%
  mutate(crimelevel = ifelse(v22e %in% c(9), NA_integer_,
                                 tolower(as.character(as_factor(v22e))))) %>%
  select(gender, age, race, weight, stiffsentences, death_penalty, crimelevel, crimerightscale) %>%
  mutate(year = 1987) %>%
  bind_rows(surveydata, .) -> surveydata



read_dta("BES_1992.dta") %>%
  mutate(gender = case_when(v915b == 1 ~ 'Male', v915b == 2 ~ "Female")) %>%
  mutate(age = v915c) %>%
  mutate(race = case_match(v915a, 1 ~ "black", c(2,3,4,6) ~ "asian", c(7,8,9) ~ "white", 5 ~ "other")) %>%
  mutate(death_penalty = case_match(v47d,
                                    1 ~ "strongly agree",
                                    2 ~ "agree",
                                    3 ~ "not sure",
                                    4 ~ "disagree",
                                    5 ~ "strongly disagree",
                                    8 ~ "dk")) %>%
  mutate(stiffsentences = case_match(v47e,
                                      1 ~ "strongly agree",
                                      2 ~ "agree",
                                      3 ~ "not sure",
                                      4 ~ "disagree",
                                      5 ~ "strongly disagree",
                                      8 ~ "dk")) %>%
  mutate(crimelevel = case_match(v26e,
                                 1 ~ "increased a lot",
                                 2 ~ "increased a little",
                                 3 ~ "same",
                                 4 ~ "fallen a little",
                                 5 ~ "fallen a lot",
                                 8 ~ "dk")) %>%
  mutate(weight = wtfactor) %>%
  select(gender, age, race, weight, stiffsentences, death_penalty, crimelevel) %>%
  mutate(year = 1992) %>%
  bind_rows(surveydata, .) -> surveydata



read_dta("BES_1997.dta") %>%
  mutate(gender = case_when(rsex == 1 ~ 'Male', rsex == 2 ~ "Female")) %>%
  mutate(age = ifelse(rage %in% c(0, 98, 99), NA_integer_, rage)) %>%
  mutate(race = case_match(ethnr, 1 ~ "white", 2 ~ 'black', 3 ~ 'asian', 4 ~ "other")) %>%
  mutate(weight = wtallgb) %>%
  mutate(death_penalty = ifelse(deathpen %in% c(9), NA_integer_,
                              tolower(as.character(as_factor(deathpen))))) %>%
  mutate(stiffsentences = ifelse(stifsent %in% c(9), NA_integer_,
                                 tolower(as.character(as_factor(stifsent))))) %>%
  mutate(crimelevel = ifelse(crimeinc %in% c(9), NA_integer_,
                             tolower(as.character(as_factor(crimeinc))))) %>%
  select(gender, age, race, weight, death_penalty, stiffsentences, crimelevel) %>%
  mutate(year = 1997) %>%
  bind_rows(surveydata, .) -> surveydata



read_dta("BES_2001prepost.dta") %>%
  filter(prepost == 1 ) %>%
  mutate(gender = case_when(genall == 1 ~ 'Male', genall == 2 ~ "Female")) %>%
  mutate(age = ifelse(ageall %in% c(0, 98, 99), NA_integer_, ageall)) %>%
  mutate(race = case_match(ethns, c(1,2)~"white", c(3,4,5,6,14,15)~"other", c(7,8,9,10)~"asian",c(11,12,13)~"black")) %>%
  mutate(weight = bpanwgt) %>%
  mutate(MII_crime1 = ifelse(bisssum %in% c(99,999), NA_integer_,
                            ifelse(bisssum == 3, 1, 0))) %>%
  select(gender, age, race, weight, MII_crime1) %>%
  mutate(year = 2001) %>%
  bind_rows(surveydata, .) -> surveydata

read_dta("BES_2001prepost.dta") %>%
  filter(mailfilt == 1 ) %>%
  mutate(gender = case_when(genall == 1 ~ 'Male', genall == 2 ~ "Female")) %>%
  mutate(age = ifelse(ageall %in% c(0, 98, 99), NA_integer_, ageall)) %>%
  mutate(race = case_match(ethns, c(1,2)~"white", c(3,4,5,6,14,15)~"other", c(7,8,9,10)~"asian",c(11,12,13)~"black")) %>%
  mutate(weight = mailwgt) %>%
  mutate(violentcrimnorights = case_match(cq15b,
                                         1 ~ "strongly agree",
                                         2 ~ "agree",
                                         3 ~ "not sure",
                                         4 ~ "disagree",
                                         5 ~ "strongly disagree",
                                         999 ~ NA_character_)) %>%
  mutate(rehabcriminalsnotpunish = case_match(cq15c,
                                             1 ~ "strongly agree",
                                             2 ~ "agree",
                                             3 ~ "not sure",
                                             4 ~ "disagree",
                                             5 ~ "strongly disagree",
                                             999 ~ NA_character_)) %>%
  mutate(stiffsentences = case_match(cq15d,
                                   1 ~ "strongly agree",
                                   2 ~ "agree",
                                   3 ~ "not sure",
                                   4 ~ "disagree",
                                   5 ~ "strongly disagree",
                                   999 ~ NA_character_)) %>%
  mutate(deathpenneverjust = case_match(cq15a,
                                   1 ~ "strongly agree",
                                   2 ~ "agree",
                                   3 ~ "not sure",
                                   4 ~ "disagree",
                                   5 ~ "strongly disagree",
                                   999 ~ NA_character_)) %>%
  mutate(crimerightscale = ifelse(cq13a == 999, NA_integer_, 
                                  ifelse(cq13a == 99, 'dk', (cq13a*-1)+10))) %>%
  select(gender, age, race, weight, 
         violentcrimnorights, rehabcriminalsnotpunish, stiffsentences, deathpenneverjust,
         crimerightscale) %>%
  mutate(year = 2001) %>%
  bind_rows(surveydata, .) -> surveydata


read_dta("BES_2005post.dta") %>%
  filter(wave2 > 1) %>%
  mutate(gender = case_when(tq76 == 1 ~ 'Male', tq76 == 2 ~ "Female")) %>%
  mutate(race = case_match(tq108, c(1,2)~"white", c(2,3,4,5,14,15)~"other", c(7,8,9,10)~"asian", c(11,12,13)~"black")) %>%
  mutate(age = tq77) %>%
  mutate(weight = postwtbr) %>%
  mutate(crimerightscale = ifelse(bq40a %in% c(98,999), NA_integer_, 
                                  ifelse(bq40a == 99, 'dk', (cq13a*-1)+10))) %>%
  mutate(crimelevel = ifelse(bq34a %in% c(99, 8), NA_integer_,
                           ifelse(bq34a == 9, 'dk', tolower(as.character(as_factor(bq34a)))))) %>%
  mutate(trstpol = ifelse(bq20d %in% c(999), NA_integer_,
                           ifelse(bq20d == 99, 'dk', bq20d))) %>% # 0 = no trust, 10 = great trust
  mutate(MII_crime1 = ifelse(bq2amisz %in% c(99,999), NA_integer_,
                            ifelse(bq2amisz == 4, 1, 0))) %>%
  select(gender, age, weight, race,
         crimerightscale, crimelevel, MII_crime1, trstpol) %>%
  mutate(year = 2005) %>%
  bind_rows(surveydata, .) -> surveydata

read_dta("BES_2005post.dta") %>%
  filter(wave2 %in% c(2,4)) %>%
  mutate(gender = case_when(tq76 == 1 ~ 'Male', tq76 == 2 ~ "Female")) %>%
  mutate(age = tq77) %>%
  mutate(race = case_match(tq108, c(1,2)~"white", c(2,3,4,5,14,15)~"other", c(7,8,9,10)~"asian", c(11,12,13)~"black")) %>%
  mutate(weight = cscwtbr) %>%
  mutate(deathpenneverjust = ifelse(cq12a %in% c(9,99), NA_integer_, 
                                    tolower(as.character(as_factor(cq12a))))) %>%
  mutate(violentcrimnorights = ifelse(cq12b %in% c(9,99), NA_integer_,
                                      tolower(as.character(as_factor(cq12b))))) %>%
  mutate(rehabcriminalsnotpunish = ifelse(cq12c %in% c(9,99), NA_integer_,
                                      tolower(as.character(as_factor(cq12c))))) %>%
  mutate(stiffsentences = ifelse(cq12d %in% c(9,99), NA_integer_,
                                          tolower(as.character(as_factor(cq12d))))) %>%
  mutate(trstcrts = ifelse(cq15g %in% c(99, 999), NA_integer_, cq15g)) %>% # 0 = no trust, 10 = great trust
  select(gender, age, race, weight, 
         violentcrimnorights, rehabcriminalsnotpunish, stiffsentences, deathpenneverjust,
         trstcrts) %>%
  mutate(year = 2005) %>%
  bind_rows(surveydata, .) -> surveydata


read_dta("BES_2010prepost.dta") %>% # data seems to be incomplete so only use the a variables (pre election sample)
  filter(is.na(aweightt) == FALSE) %>%
  mutate(gender = case_when(aq62 == 1 ~ 'Male', aq62 == 2 ~ "Female")) %>%
  mutate(race = case_match(aq75, c(1,2)~"white", c(3,4,5,6,14,15)~"other", c(7,8,9,10)~"asian", c(11,12,13)~"black")) %>%
  mutate(age = ifelse(aq63 %in% c(-1,-2), NA_integer_, aq63)) %>%
  mutate(weight = aweightt) %>%
  mutate(MII_crime1 = ifelse(aq2 %in% c(-2), NA_integer_,
                            ifelse(aq2 == 4, 1, 0))) %>%
  mutate(crimerightscale = as.character(ifelse(aq38_1 == 12, 'dk', (aq38_1*-1)+10))) %>%
  mutate(trstpol = as.character(ifelse(aq15_3 == 12, 'dk', aq15_3))) %>% # 0 = no trust, 10 = great trust
  mutate(trstcrts = ifelse(aq15_4 %in% c(12), 'dk', aq15_4)) %>% # 0 = no trust, 10 = great trust
  select(gender, age, race, weight, 
         MII_crime1, crimerightscale,
         trstpol, trstcrts) %>%
  mutate(year = 2010) %>%
  bind_rows(surveydata, .) -> surveydata

  
read_dta("BES_2015.dta") %>%
  mutate(gender = case_when(y09 == 1 ~ 'Male', y09 == 2 ~ "Female")) %>%
  mutate(age = Age) %>%
  mutate(race = case_match(y11, c(1,2,3,4)~"white", c(5,6,7,8,12,17,18)~"other", c(9,10,11,13)~"asian", c(14,15,16)~'black')) %>%
  mutate(weight = wt_combined_main) %>%
  mutate(MII_crime2 = ifelse(is.na(a1_mii), NA_integer_,
                            ifelse(a1_mii == 17, 1, 0))) %>%
  select(gender, age, race, weight, 
         MII_crime2) %>%
  mutate(year = 2015) %>%
  bind_rows(surveydata, .) -> surveydata


read_dta("BES_2017.dta") %>%
  mutate(gender = case_when(y09 == 1 ~ 'Male', y09 == 2 ~ "Female")) %>%
  mutate(age = ifelse(Age == -2, NA_integer_, Age)) %>%
  mutate(race = case_match(y11, c(1,2,3,4,19)~"white", c(5,6,7,8,12,17,18)~"other", c(9,10,11,13)~"asian", c(14,15,16)~'black')) %>%
  mutate(weight = wt_demog) %>%
  mutate(MII_crime2 = ifelse(is.na(mii_coded), NA_integer_,
                            ifelse(mii_coded == 14, 1, 0))) %>%
  mutate(death_penalty = case_match(f01_11,
                                -999 ~ NA_character_,
                                -1 ~ 'dk',
                                1 ~ "strongly disagree",
                                2 ~ "disagree",
                                3 ~ "not sure",
                                4 ~ "agree",
                                5 ~ "strongly agree")) %>%
  mutate(stiffsentences = case_match(f01_12,
                                -999 ~ NA_character_,
                                -1 ~ 'dk',
                                1 ~ "strongly disagree",
                                2 ~ "disagree",
                                3 ~ "not sure",
                                4 ~ "agree",
                                5 ~ "strongly agree")) %>%
  select(gender, age, weight, race,
         MII_crime2, death_penalty, stiffsentences) %>%
  mutate(year = 2017) %>%
  bind_rows(surveydata, .) -> surveydata

read_dta("BES_2019.dta") %>%
  mutate(gender = case_when(y09 == 1 ~ 'Male', y09 == 2 ~ "Female")) %>%
  mutate(age = ifelse(Age %in% c(-2,-999), NA_integer_, Age)) %>%
  mutate(race = case_match(y11, c(1,2,3,4)~"white", c(5,6,7,8,12,17,18)~"other", c(9,10,11,13)~"asian", c(14,15,16)~'black')) %>%
  mutate(weight = wt_demog) %>%
  mutate(MII_crime2 = ifelse(is.na(a01_code), NA_integer_,
                             ifelse(a01_code == 6, 1, 0))) %>%
  mutate(death_penalty = case_match(f01_11,
                                    -999 ~ NA_character_,
                                    -1 ~ 'dk',
                                    1 ~ "strongly disagree",
                                    2 ~ "disagree",
                                    3 ~ "not sure",
                                    4 ~ "agree",
                                    5 ~ "strongly agree")) %>%
  mutate(stiffsentences = case_match(f01_12,
                                    -999 ~ NA_character_,
                                    -1 ~ 'dk',
                                    1 ~ "strongly disagree",
                                    2 ~ "disagree",
                                    3 ~ "not sure",
                                    4 ~ "agree",
                                    5 ~ "strongly agree")) %>%
  select(gender, age, weight, race,
         MII_crime2, death_penalty, stiffsentences) %>%
  mutate(year = 2019) %>%
  bind_rows(surveydata, .) -> surveydata

write_csv(surveydata, "/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Survey Data/Finished/BESmain_allrepeatedcrimequestions.csv")



######### BES Panels ###########

setwd("/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Survey Data/Originals/BES") 

### The 1986-87 panel is a 3-wave follow up to the 1983 election pre and post the 1987 election [1986 and 1987]
# q41a[1] "[SCALE] LAW AND ORDER: R'S VIEW"
# q42a[1] "AGREE,DISAGREE: CUT UNEMPLYT CUTS CRIME"
# q42b[1] "AGREE,DISAGREE: MORE POWER FOR POLICE"
# q42c[1] "AGREE,DISAGREE: BRING BACK DEATH PENALTY"
# q42d[1] "AGREE,DISAGREE: PRISONS SHOULD REFORM"
# q42e[1] "AGREE,DISAGREE: NEED STIFFER SENTENCES"

read_sav("1983-1987 BES Panelw01.sav") %>%
  mutate(age = na_if(q56, 99)) %>%
  mutate(q64b = na_if(q64b, 9)) %>%
  mutate(gender = case_match(q64b, 1 ~ 'Male', 2 ~ 'Female')) %>%
  mutate(race = case_match(q64a, 1 ~ "white", 2 ~ "asian", 3 ~ "black", 4 ~ "other")) %>%
  select(age, gender, weight, race,
         q41a, q42c, q42e) %>%  
  mutate(crimerightscale = ifelse(q41a %in% c(95, 96, 99), NA_integer_,
                                  ifelse(q41a == 98, "dk", (q41a+10)/2))) %>% # -10 protect rights, 10 cut crime
  mutate(across(c(q42c, q42e),
                ~if_else(. %in% c(9), NA_integer_, .))) %>%
  mutate(across(c(q42c, q42e), ~tolower(as.character(as_factor(.))))) %>%
  rename(death_penalty = q42c, stiffsentences = q42e) %>%
  mutate(year = 1986) %>%
  select(year, age, gender, race, weight, death_penalty, stiffsentences, crimerightscale) -> paneldata

read_sav("1983-1987 BES Panelw012.sav") %>%
  mutate(age = na_if(q56, 99)) %>%
  mutate(q64b = na_if(q64b, 9)) %>%
  mutate(gender = case_match(q64b, 1 ~ 'Male', 2 ~ 'Female')) %>%
  mutate(race = case_match(q64a, 1 ~ "white", 2 ~ "asian", 3 ~ "black", 4 ~ "other", 9 ~ NA_character_)) %>%
  select(age, gender, weight, race,
         q41a, q42c, q42e) %>%  
  mutate(crimerightscale = ifelse(q41a %in% c(95, 96, 99), NA_integer_,
                                  ifelse(q41a == 98, "dk", (q41a+10)/2))) %>% # -10 protect rights, 10 cut crime
  mutate(across(c(q42c, q42e),
                ~if_else(. %in% c(9), NA_integer_, .))) %>%
  mutate(across(c(q42c, q42e), ~tolower(as.character(as_factor(.))))) %>%
  rename(death_penalty = q42c, stiffsentences = q42e) %>%
  mutate(year = 1987) %>%
  select(year, age, gender, race, weight, death_penalty, stiffsentences, crimerightscale) %>%
  bind_rows(paneldata, .) -> paneldata


### The 1987-92 panel is a 5-wave follow up to the 1987 election with 5 interviews taking place over 1992
# y22e[1] "since '87 elec,increased/fallen:crime" = wave234, wave4
# y212c[1] "break the law shd be given stiffer sent?" = wave2345, wave45
# y212e[1] "some crime,death pen appropriate sentnc?"
# y212g[1] "law should be obeyed evn if law is wrong?"
# y322a[1] "respondent's sex (interviewer code)"
# y322b[1] "respondent's age last birthday"    

read_sav("1987-1992 BES Panel.sav") %>%
  filter(wave4 == 1) %>%
  mutate(age = na_if(y322b, 99)) %>%
  mutate(y322a = na_if(y322a, 9)) %>%
  mutate(gender = case_match(y322a, 1 ~ 'Male', 2 ~ 'Female')) %>%
  mutate(race = case_match(v58a, 1 ~ "white", 2 ~ "asian", 3 ~ "black", 4 ~ "other", c(9, -1) ~ NA_character_)) %>%
  mutate(weight = wt14) %>%
  mutate(crimelevel = ifelse(y22e %in% c(9), NA_integer_,
                                  ifelse(y22e == 8, "dk", tolower(as.character(as_factor(y22e)))))) %>%
  select(age, gender, race, weight, crimelevel) %>%  
  mutate(year = 1992) %>%
  bind_rows(paneldata, .) -> paneldata

read_sav("1987-1992 BES Panel.sav") %>%
  filter(wave45 == 1) %>%
  mutate(weight = wt145) %>%
  mutate(age = na_if(y322b, 99)) %>%
  mutate(y322a = na_if(y322a, 9)) %>%
  mutate(gender = case_match(y322a, 1 ~ 'Male', 2 ~ 'Female')) %>%
  mutate(race = case_match(v58a, 1 ~ "white", 2 ~ "asian", 3 ~ "black", 4 ~ "other")) %>%
  mutate(across(c(y212c, y212e),
                ~if_else(. %in% c(9, -2), NA_integer_, .))) %>%
  mutate(across(c(y212c, y212e), ~tolower(as.character(as_factor(.))))) %>%
  rename(stiffsentences = y212c, death_penalty = y212e) %>%
  select(age, gender, race, weight, stiffsentences, death_penalty) %>%  
  mutate(year = 1992) %>%
  bind_rows(paneldata, .) -> paneldata

### British Election Panel Study, 1992-1997
# crminc93[1] "Crime increased since last G.Elec?   R3d"
# cntcri94[1] "Control of crime in this area 2yrS31aiii"
# cntcri95[1] "Control of crime in this area 2yrT17aiii"
# deathp94[1] "GB should bring back death penalty?S201b"
# stifsn94[1] "Lawbreakers sh get stiffer sentenceS201c"
# rsex93 rsex94 / rage93 rage94

read_sav("1992-1997 BES Panel.sav") %>%
  mutate(weight = wtfactor) %>%
  drop_na(crminc93) %>% 
  mutate(rage93 = na_if(rage93, 99)) %>%
  mutate(age = na_if(rage93, -2)) %>%
  mutate(rsex93 = na_if(rsex93, 9)) %>%
  mutate(rsex93 = na_if(rsex93, -2)) %>%
  mutate(race = case_match(raceor92, 1 ~ "black", c(2,3,4,6) ~ "asian", c(7,8,9) ~ "white", c(5,10) ~ "other")) %>%
  mutate(gender = case_match(rsex93, 1 ~ 'Male', 2 ~ 'Female')) %>%
  mutate(across(c(crminc93),
                ~if_else(. %in% c(9, -1), NA_integer_, .))) %>%
  mutate(across(c(crminc93), ~tolower(as.character(as_factor(.))))) %>%
  rename(crimelevel = crminc93) %>%
  filter(!(is.na(crimelevel))) %>%
  select(age, gender, race, weight, crimelevel) %>%  
  mutate(year = 1993) %>%
  bind_rows(paneldata, .) -> paneldata

read_sav("1992-1997 BES Panel.sav") %>%
  mutate(weight = wtfactor) %>%
  drop_na(stifsn94) %>%
  mutate(rage94 = na_if(rage94, 99)) %>%
  mutate(age = na_if(rage94, -2)) %>%
  mutate(rsex94 = na_if(rsex94, 9)) %>%
  mutate(rsex94 = na_if(rsex94, -2)) %>%
  mutate(race = case_match(raceor92, 1 ~ "black", c(2,3,4,6) ~ "asian", c(7,8,9) ~ "white", c(5,10) ~ "other")) %>%
  mutate(gender = case_match(rsex94, 1 ~ 'Male', 2 ~ 'Female')) %>%
  mutate(across(c(stifsn94, deathp94),
                ~if_else(. %in% c(9, -1), NA_integer_, .))) %>%
  mutate(across(c(stifsn94, deathp94), ~tolower(as.character(as_factor(.))))) %>%
  rename(stiffsentences = stifsn94, death_penalty = deathp94) %>%
  select(age, gender, race, weight, stiffsentences, death_penalty) %>%  
  mutate(year = 1994) %>%
  bind_rows(paneldata, .) -> paneldata


### British Election Panel Study, 1997-2001; Waves 1 to 8
# Only asked relevant q in 1998
# crimei98[1] "Crime increased since last G.Elec?   T82"
# rsex98[1] "Respondent's sex by observation     T197"
# rage98[1] "Respondent's age last birthday?[yrs]T198"
# wtallgb[1] "Weight:whole sample:Britain  Spring'97 Q"

read_sav("1997-2001 BES Panel.sav") %>%
  mutate(weight = wtallgb) %>%
  drop_na(crimei98) %>% 
  mutate(rage98 = na_if(rage98, 999)) %>%
  mutate(rage98 = na_if(rage98, 998)) %>%
  mutate(age = na_if(rage98, -8)) %>%
  mutate(rsex98 = na_if(rsex98, 9)) %>%
  mutate(rsex98 = na_if(rsex98, -8)) %>%
  mutate(race = case_match(raceor97, c(1,2,3) ~ "black", c(4,5,6,8) ~ "asian", c(9,10) ~ "white", c(7,11,12) ~ "other")) %>%
  mutate(gender = case_match(rsex98, 1 ~ 'Male', 2 ~ 'Female')) %>%
  mutate(across(c(crimei98),
                ~if_else(. %in% c(9, -8), NA_integer_, .))) %>%
  mutate(across(c(crimei98), ~tolower(as.character(as_factor(.))))) %>%
  rename(crimelevel = crimei98) %>%
  filter(!(is.na(crimelevel))) %>%
  select(age, gender, race, weight, crimelevel) %>%  
  mutate(year = 1998) %>% 
  bind_rows(paneldata, .) -> paneldata


### British Election Study Nine-Wave Panel Survey, 2005-2010
# oyo_q80[1] "Crime-Rights Scale-Self"
# y09_q58[1] "More Impt-Crime-Rights-Self"
# oyo_q101[1] "Age Band" 
# y09_q84[1] "Age Band"
# oyo_q102[1] "Gender"
# y09_q85[1] "Gender"
# oyo_w8no[1] "Nonpolitical YouGov Weight"
# y09_w8_n[1] "Nonpolitical YouGov Weighting"

read_sav("2005-2009 BES Panel.sav") %>%
  mutate(weight = oyo_w8no) %>%
  drop_na(oyo_w8no) %>%
  mutate(oyo_q100 = na_if(oyo_q100, 999)) %>%
  mutate(oyo_q100 = na_if(oyo_q100, 998)) %>%
  mutate(age = 2006 - as.numeric(as.character(as_factor(oyo_q100)))) %>%
  mutate(oyo_q102 = na_if(oyo_q102, 99)) %>%
  mutate(gender = case_match(oyo_q102, 1 ~ 'Male', 2 ~ 'Female')) %>%
  mutate(race = case_match(pre_q174, 1 ~ "white", c(2,5) ~ "other", 3 ~ "asian", 4 ~ "black")) %>%
  mutate(across(c(oyo_q80),
                ~if_else(. %in% c(99), NA_integer_, .))) %>%
  mutate(crimerightscale = as.character(ifelse(oyo_q80 == 12, 'dk', (oyo_q80*-1)+10))) %>% # 0 protect rights, 10 punish criminals
  select(age, gender, race, weight, crimerightscale) %>%  
  mutate(year = 2006) %>%
  bind_rows(paneldata, .) -> paneldata

read_sav("2005-2009 BES Panel.sav") %>%
  mutate(weight = y09_w8_n) %>%
  drop_na(y09_w8_n) %>% 
  mutate(y09_q83 = na_if(y09_q83, 93)) %>%
  mutate(age = 2009-as.numeric(as.character(as_factor(y09_q83)))) %>%
  mutate(y09_q85 = na_if(y09_q85, 99)) %>%
  mutate(gender = case_match(y09_q85, 1 ~ 'Male', 2 ~ 'Female')) %>%
  mutate(race = case_match(pre_q174, 1 ~ "white", c(2,5) ~ "other", 3 ~ "asian", 4 ~ "black")) %>%
  mutate(across(c(y09_q58),
                ~if_else(. %in% c(99), NA_integer_, .))) %>%
  mutate(crimerightscale = as.character(ifelse(y09_q58 == 12, 'dk', (y09_q58*-1)+10))) %>% # 0 protect rights, 10 punish criminals
  select(age, gender, race, weight, crimerightscale) %>%  
  mutate(year = 2009) %>%
  bind_rows(paneldata, .) %>%
  mutate(year = as.character(year)) -> paneldata


### BES Continuous Monitoring Survey is a monthly survey from 2004-2010
# dates = 2004-04 to 2010-12
# year = substr(yrmon,2,3)
# month = substr(yrmon,4,5)
# q2[1] "Gender"
# q3[1] "Date of Birth"
# q55[1] "Crime Situation These Days"
# q74_1[1] "Emotion-Crime-Angry"
# q74_3[1] "Emotion-Crime-Disgusted"
# q74_5[1] "Emotion-Crime-Uneasy"
# q74_7[1] "Emotion-Crime-Afraid"
# w8nonpol[1] "Nonpolitical Weight"

haven::read_dta("2003-2010 BES CMS.dta", encoding = 'latin1') %>%
  mutate(weight = w8nonpol) %>%
  mutate(year = paste0('20',substr(yrmon,2,3)))  %>% 
  mutate(q3 = ifelse(q3 < 11, 11, q3)) %>%
  mutate(age = as.numeric(paste0('20',substr(yrmon,2,3)))-as.numeric(paste0('19',q3-1))) %>%
  mutate(gender = case_match(q2, 1 ~ 'Male', 2 ~ 'Female')) %>%
  mutate(race = case_match(q120, c(1,2) ~ "white", c(3,4,5,6,14,15) ~ "other", c(7,8,9,10) ~ "asian", c(11,12,13) ~ "black")) %>%
  mutate(crimelevel = tolower(as.character(as_factor(q55)))) %>%
  rename(Emotion_Crime_Angry = q74_1, Emotion_Crime_Disgusted = q74_3,
         Emotion_Crime_Uneasy = q74_5, Emotion_Crime_Afraid = q74_7) %>%
  select(age, gender, race, weight, year, crimelevel, Emotion_Crime_Angry, Emotion_Crime_Disgusted,
         Emotion_Crime_Uneasy, Emotion_Crime_Afraid) %>%
  bind_rows(paneldata, .) -> paneldata

paneldata |> mutate(year = as.numeric(year)) -> paneldata

write_csv(paneldata, "/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Survey Data/Finished/BESpanels_allrepeatedcrimequestions.csv")

